package conf

import (
	"fmt"
	"gopkg.in/ini.v1"
	"strings"
)

var (
	AppMode      string
	HttpPort     string
	Db           string
	DbHost       string
	DbPort       string
	DbUser       string
	DbPassWord   string
	DbNamestring string
	DBdns        string
	IP           string
	Level        string
	Filename     string
	MaxSize      int
	MaxAge       int
	MaxBackups   int
)

func init() {
	file, err := ini.Load("./conf/config.ini")
	if err != nil {
		fmt.Println("配置文件读取错误，请检查文件路径")
		return
	}
	LoadServer(file)
	LoadMysql(file)
	LoadLog(file)
	DBdns = strings.Join([]string{DbUser, ":", DbPassWord, "@tcp(", DbHost, ":", DbPort, ")/", DbNamestring, "?charset=utf8mb4&parseTime=True&loc=Local"}, "")
}
func LoadServer(file *ini.File) {
	AppMode = file.Section("service").Key("AppMode").String()
	HttpPort = file.Section("service").Key("HttpPort").String()
	IP = file.Section("service").Key("IP").String()
}
func LoadMysql(file *ini.File) {
	Db = file.Section("mysql").Key("Db").String()
	DbHost = file.Section("mysql").Key("DbHost").String()
	DbPort = file.Section("mysql").Key("DbPort").String()
	DbUser = file.Section("mysql").Key("DbUser").String()
	DbPassWord = file.Section("mysql").Key("DbPassWord").String()
	DbNamestring = file.Section("mysql").Key("DbName").String()

}
func LoadLog(file *ini.File) {
	Level = file.Section("log").Key("Level").String()
	Filename = file.Section("log").Key("Filename").String()
	MaxSize, _ = file.Section("log").Key("MaxSize").Int()
	MaxAge, _ = file.Section("log").Key("MaxAge").Int()
	MaxBackups, _ = file.Section("log").Key("MaxBackups").Int()
}
